<?php
class UserAction extends Action {

	public function index() {

	}

	public function userManagement() {
		$Dao = M('student');
		$students = $Dao -> select();
		foreach ($students as $key => $value) {
			$group_id = $value['group_id'];
			$group_dao = M('group');
			$condition['id'] = $group_id;
			$group_result = $group_dao->where($condition)->find();
			$students[$key]['group_name'] = $group_result['name'];
		}
		
		
		//$students = array_sort($students,'group_name');
		for($i=0; $i<count($students) ;$i++){
			for($j=0; $j<$i; $j++){
				if($students[$i]['group_name'] > $students[$j]['group_name']){
				$temp = $students[$i];
				$students[$i] = $students[$j];
				$students[$j] = $temp;
				}
			}
        }
		$this -> assign('students', $students);
		$this -> display();
	}

	public function delete() {
		//header("Content-Type:text/html; charset=utf-8");
		$Dao = M('student');
		$condition['id'] = $_GET['stu_id'];
		
		if ($Dao -> where($condition) -> delete()) {
			//echo '用户删除成功！';
			$this->ajaxReturn(null, null , 1);
		} else {
			//echo '用户删除失败！';
			$this->ajaxReturn(null, null, 0);
		}

	}

	public function addStudent() {
		$Dao = D('student');
		if (!$Dao -> create()) {
			$this -> ajaxReturn(null, $Dao -> getError(), 0);
		} else {
			if ($Dao -> add()) {
				$this -> ajaxReturn(null, '用户添加成功', 1);
			} else {
				$this -> ajaxReturn(null, '用户添加失败', 0);
			}
		}
		//$this -> ajaxReturn(null, null, 1);
	}

}
?>